<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class RoleController extends Controller{
    //用户页面显示
    public function role(){
      return  view('admin.role');
    }
    //用户显示
    public function role_table(){
        $rows=Db::table('role')->get();
        $data=[
            "code"=>0,
            "msg"=>'',
            "count"=>count($rows),
            "data"=>$rows
        ];
        return json_encode($data);
    }
    public function sort($data)         //递归获取无限极分类结构树
    {
        $tree = [];
        $newData = [];
        //循环重新排列
        foreach ($data as $datum) {
            $newData[$datum->id] = $datum;
        }
        foreach ($newData as $key => $datum) {
            if ($datum->pid > 0) {
                //不是根节点的将自己的地址放到父级的children节点
                $newData[$datum->pid]->list[] = &$newData[$key];
            } else {
                //根节点直接把地址放到新数组中
                $tree[] = &$newData[$datum->id];
            }
        }
        return $tree;
    }
    public function role_json(){
        $rows=Db::table('node')
            ->select(['id','pid','title as name'])
            ->get();
        $data=$this->sort($rows);
        $data=[
            "code"=>0,
            "msg"=>'',
            "data"=>["trees"=>$data]
        ];
        return json_encode($data);
    }
    public function role_insert(){
        $data=Input::get('result');
        $node_id=Input::get('authids');
        $str=implode(',',$node_id);
        $arr['role_name']=$data['role_name'];
        $arr['node_id']=$str;
        $rows=DB::table('role')->insert($arr);
        if($rows){
            return 1;
        }else{
            return 0;
        }
    }
    public function role_edit($id){
        $rows=DB::table('role')->where('id',$id)->first();
        return view('admin.role_edit',['rows'=>$rows]);
    }
    public function role_edit_json($id){
        $role=DB::table('role')->where('id',$id)->value('node_id');
        $rows=Db::table('node')
            ->select(['id','pid','title as name'])
            ->get();
        $str = explode(",",$role);
        for($i=0;$i<count($rows);$i++){
            foreach($str as $v2){
                if($rows[$i]->id == $v2){
                    $rows[$i]->checked = true;
                }
            }
        }
        $data=$this->sort($rows);
        $data=[
            "code"=>0,
            "msg"=>'',
            "data"=>["trees"=>$data]
        ];
        return json_encode($data);
    }
    public function role_update(Request $request){
        $id=$request->post('id');
        $data=$request->post('result');
        $node_id=$request->post('authids');
        $str=implode(',',$node_id);
        $arr['role_name']=$data['role_name'];
        $arr['node_id']=$str;
        $rows=DB::table('role')->where('id',$id)->update($arr);
        if($rows){
            return 1;
        }
    }

}